<#include "/common/_layout.html"/>
<@layout>
	<style>

	</style>
	<div id="app">
		<el-form ref="form" :rules="rules" :model="form" label-width="100px" v-loading="loading">
			<el-row :gutter="20" style="margin: 0;">
				<el-col :span="12" :offset="5" style="padding: 0;">
					<el-form-item label="类型名称" prop="type_name">
						<el-input v-model.trim="form.type_name"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>

		<el-row class="lb_form_button">
			<el-button type="primary" @click="submitForm('form')">提交</el-button>
			<el-button type="danger" @click="closeDlalog">关闭</el-button>
		</el-row>
	</div>

	<script type="text/javascript">
		var _vue = new Vue({
			el: '#app',
			data: function() {
				return {
					form: {
						type_name: "",
					},
					rules: {
						type_name: [{
							required: true,
							message: '请输入名称',
							trigger: 'blur'
						}, {
							trigger: 'blur',
							validator: function(rule, value, callback) {
								$.ajax({
									type: "post",
									url: "${ctx}/goodsType/judge",
									data: {
										'typeName': value
									},
									success: function(data) {
										if(data.result.isHas) {
											callback(new Error("类型名称已存在"));
										} else {
											callback();
										}
									}
								});
							}
						}]
					},
					loading: false
				}
			},
			methods: {
				submitForm: function(formName) {
					var params = {};
					$.each(this[formName], function(key, value) {
						params["type." + key] = value;
					});
					params["type.id"] = parent.selectData.id;
					FormUtils.submit(this, formName, "${ctx}/goodsType/update", params);
				},
				closeDlalog: function() {
					//关闭当前这个iframe的dialog
					parent._vue[window.frameElement.id].dialogVisible = false;
				}
			},
			mounted: function() {
				if(!!parent.selectData) {
					var selectData = parent.selectData;
					var _that = this;
					// 通用遍历form赋初始值的方法
					$.each(selectData, function(key, value) {
						if(_that.form[key] != undefined) {
							_that.form[key] = value;
						}
					});
				} else {
					parent._vue.$message({
						message: "发生错误,无法编辑",
						type: 'error'
					});
					this.closeDlalog();
				}
			}
		});
	</script>
</@layout>